package model

import (
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var (
	db  *gorm.DB
	err error
)

func init() {
	dsn := "root:123456@tcp(127.0.0.1:3306)/2108a?charset=utf8mb4&parseTime=True&loc=Local"
	db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		logx.Error(err)
		return
	}
	db.AutoMigrate(new(User))
}

// User 用户表
type User struct {
	gorm.Model
	Username string `gorm:"column:username;type:varchar(50);not null;unique_index:username" json:"username"`
	Password string `gorm:"column:password;type:varchar(255);not null" json:"password"`
	Nickname string `gorm:"column:nickname;type:varchar(50);" json:"nickname"`
	Avatar   string `gorm:"column:avatar;type:varchar(255);" json:"avatar"`
	Status   int    `gorm:"column:status;type:tinyint(1);not null" json:"status"`
}

func UserRegister(user *User) (*User, error) {
	res := db.Create(&user)
	return user, res.Error
}
func CheckUser(user *User) (*User, error) {
	newUser := &User{}
	res := db.Where(user).First(&newUser)
	return newUser, res.Error
}
